package com.xiyili.topspecialty;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.xiyili.topspecialty.data.FineCourse;
import com.xiyili.topspecialty.data.Specialty;
import com.xiyili.topspecialty.data.SpecialtyDatabaseHelper;

/**
 * A fragment representing a single Specialty detail screen. This fragment is
 * either contained in a {@link SpecialtyListActivity} in two-pane mode (on
 * tablets) or a {@link TopSchoolListActivity} on handsets.
 */
public class TopSpecialtyListFragment extends ListFragment {
	/**
	 * The fragment argument representing the item ID that this fragment
	 * represents.
	 */
	public static final String EXTRA_SCHOOL_CODE = "school_code";
	public static final String EXTRA_SCHOOL_NAME = "school_name";
	private String mSchoolCode;
	private String mSchoolName;



	/**
	 * Mandatory empty constructor for the fragment manager to instantiate the
	 * fragment (e.g. upon screen orientation changes).
	 */
	public TopSpecialtyListFragment() {
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mSchoolCode = getArguments().getString(
				EXTRA_SCHOOL_CODE);
		mSchoolName = getArguments().getString(EXTRA_SCHOOL_NAME);

	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		List<Specialty> items =  SpecialtyDatabaseHelper.getInstance(getActivity())
				.findSpecialtyBySchoolCode(mSchoolCode);
		List<FineCourse> courses = SpecialtyDatabaseHelper.getInstance(getActivity()).findFindCoursesBySchoolName(mSchoolName);
		if((courses != null) && (courses.size()>0)) {
			View footer =	getLayoutInflater(savedInstanceState).inflate(R.layout.school_top_specialties_footer, getListView(), false);
			footer.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					showFineCoursesBySchoolName();
				}
			});
			getListView().addHeaderView(footer);
		}
		setListAdapter(new SpecialtyAdapter(getActivity(), items));
	}

	protected void showFineCoursesBySchoolName() {
		Intent intent = new Intent(getActivity(),FineCourseListActivity.class);
		intent.putExtra(EXTRA_SCHOOL_NAME, mSchoolName);
		startActivity(intent);

	}

	static class SpecialtyAdapter extends BaseAdapter{
		private final LayoutInflater mInflater;
		private final List<Specialty> mData;
		public SpecialtyAdapter(Context context,List<Specialty> data) {
			mInflater = LayoutInflater.from(context);
			mData = data;
		}

		@Override
		public int getCount() {
			return mData.size();
		}

		@Override
		public Object getItem(int position) {
			return mData.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if(convertView == null) {
				convertView = mInflater.inflate(R.layout.specialty_item, parent, false);
				holder  = new ViewHolder();
				holder.name = (TextView) convertView.findViewById(R.id.school_name);
				holder.score = (TextView) convertView.findViewById(R.id.score);

				convertView.setTag(holder);
			}else {
				holder = (ViewHolder) convertView.getTag();
			}

			Specialty item = (Specialty) getItem(position);

			holder.name.setText(item.name);
			holder.score.setText(""+item.score);


			return convertView;
		}

		static class ViewHolder{
			public TextView name;
			public TextView score;
		}



	}


}
